package com.mrx.pvzcommunity.exceptions;

import com.mrx.pvzcommunity.model.Response;
import com.mrx.pvzcommunity.model.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.util.UUID;

@ControllerAdvice(basePackages = "com.mrx.pvzcommunity.controller")
public class GlobalHandler {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @ExceptionHandler(Throwable.class)
    @ResponseStatus(code = HttpStatus.INTERNAL_SERVER_ERROR)
    public Result<String> problem(final Throwable e) {
        String message = e.getMessage();
        String uuid = UUID.randomUUID().toString();
        logger.error("uuid =" + uuid, message, e);
        return Result.createResult(Response.Code.INTERNAL_SERVER_ERROR, message, uuid);
    }
}
